package com.ruoyi.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;

import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.FanningdiMapper;
import com.ruoyi.system.mapper.ZhonggaofengxianquMapper;
import com.ruoyi.system.mapper.ZwryqkMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.FnryqkMapper;
import com.ruoyi.system.service.IFnryqkService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * fnryqkService业务层处理
 * 
 * @author ruoyi
 * @date 2021-10-28
 */
@Service
public class FnryqkServiceImpl implements IFnryqkService 
{
    @Autowired
    private FnryqkMapper fnryqkMapper;

    @Autowired
    private ZwryqkMapper zwryqkMapper;

    @Autowired
    private FanningdiMapper ddFanningdiMapper;

    @Autowired
    private ZhonggaofengxianquMapper ddZhonggaofengxianquMapper;

    /**
     * 查询fnryqk
     *
     *
     * @return fnryqk
     */
    @Override
    public List<String> selectFnryqkByUserIdAndFanningtime(String userid,String fanningtime)
    {
        return fnryqkMapper.selectFnryqkByUserIdAndFanningtime(userid,fanningtime);
    }

    /**
     * 查询fnryqk
     * 
     * @param id fnryqkID
     * @return fnryqk
     */
    @Override
    public Fnryqk selectFnryqkById(String id)
    {
        return fnryqkMapper.selectFnryqkById(id);
    }

    /**
     * 查询fnryqk列表
     * 
     * @param fnryqk fnryqk
     * @return fnryqk
     */
    @Override
    public List<Fnryqk> selectFnryqkList(Fnryqk fnryqk)
    {
        return fnryqkMapper.selectFnryqkList(fnryqk);
    }

    /**
     * 新增fnryqk
     * 
     * @param fnryqk fnryqk
     * @return 结果
     */
    @Override
    @Transactional
    public int insertFnryqk(Fnryqk fnryqk,String ppFanningdi,String ppGaofengxiandis)
    {
        int ok = 1;
        // 获得该用户id
        String mmUserId = fnryqk.getUserid();

        String mmFnryqkid = fnryqk.getId();

        Fanningdi mmFanningdi = new Fanningdi();
        mmFanningdi.setId(UUID.randomUUID().toString());
        mmFanningdi.setFnryqkid(mmFnryqkid);
        mmFanningdi.setFanningdi(ppFanningdi);
        mmFanningdi.setChuangjiantime(new Date());
        mmFanningdi.setStatus("1");
        int ok1 = ddFanningdiMapper.insertFanningdi(mmFanningdi);
        if (ok1!=1){
            ok=0;
        }

        String[] gaofengxiandi = ppGaofengxiandis.split(";");
        if (gaofengxiandi !=null && gaofengxiandi.length !=0){
            for (int i = 0;i<gaofengxiandi.length;i++){
                Zhonggaofengxianqu mmZhonggaofengxianqu = new Zhonggaofengxianqu();
                mmZhonggaofengxianqu.setId(UUID.randomUUID().toString());
                mmZhonggaofengxianqu.setFnryqkid(mmFnryqkid);
                mmZhonggaofengxianqu.setZhonggaofengxiandi(gaofengxiandi[i]);
                mmZhonggaofengxianqu.setChuangjiantime(new Date());
                mmZhonggaofengxianqu.setStatus("1");
                int ok2 = ddZhonggaofengxianquMapper.insertZhonggaofengxianqu(mmZhonggaofengxianqu);
                if (ok2!=1){
                    ok=0;
                }
            }
        }

        int ok3 = fnryqkMapper.insertFnryqk(fnryqk);
        if (ok3!=1){
            ok = 0;
        }

        if (fnryqkMapper.selectLatestTime(mmUserId)!=null&&zwryqkMapper.selectLatestTime(mmUserId)==null){
            String mmShijian1 = new SimpleDateFormat("yyyy-MM-dd").format(fnryqkMapper.selectLatestTime(mmUserId).get("shijian"));

            fnryqkMapper.updateShixiaoByUserId(mmUserId,mmShijian1);
            zwryqkMapper.updateShixiaoByUserId(mmUserId,mmShijian1);
        }

        if (zwryqkMapper.selectLatestTime(mmUserId)!=null&&fnryqkMapper.selectLatestTime(mmUserId)==null){
            String mmShijian2 = new SimpleDateFormat("yyyy-MM-dd").format(zwryqkMapper.selectLatestTime(mmUserId).get("shijian"));

            fnryqkMapper.updateShixiaoByUserId(mmUserId,mmShijian2);
            zwryqkMapper.updateShixiaoByUserId(mmUserId,mmShijian2);
        }

        if (zwryqkMapper.selectLatestTime(mmUserId)!=null&&fnryqkMapper.selectLatestTime(mmUserId)!=null){
            String mmShijian1 = new SimpleDateFormat("yyyy-MM-dd").format(fnryqkMapper.selectLatestTime(mmUserId).get("shijian"));
            String mmShijian2 = new SimpleDateFormat("yyyy-MM-dd").format(zwryqkMapper.selectLatestTime(mmUserId).get("shijian"));
            String mmChuanjian1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(fnryqkMapper.selectChuanjianTime(mmUserId,mmShijian1).get("chuangjiantime"));
            String mmChuanjian2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(zwryqkMapper.selectChuanjianTime(mmUserId,mmShijian2).get("chuangjiantime"));
            String mmShijian = "";
            String mmChuanjian = "";

            int i = mmShijian1.compareTo(mmShijian2);
            int j = mmChuanjian1.compareTo(mmChuanjian2);
            if (i==0){
                mmShijian = mmShijian1;
                if(j==0){
                    mmChuanjian = mmChuanjian1;
                }else if(j<0){
                    mmChuanjian = mmChuanjian1;

                }else if (j>0){
                    mmChuanjian = mmChuanjian2;
                }
                zwryqkMapper.updateShixiaoByUserIdAndTime(mmUserId,mmChuanjian);
                fnryqkMapper.updateShixiaoByUserIdAndTime(mmUserId,mmChuanjian);
            }else if (i<0){
                mmShijian = mmShijian2;
            }else if (i>0){
                mmShijian = mmShijian1;
            }
            fnryqkMapper.updateShixiaoByUserId(mmUserId,mmShijian);
            zwryqkMapper.updateShixiaoByUserId(mmUserId,mmShijian);
        }

        return ok;
    }

    /**
     * 修改fnryqk
     * 
     * @param fnryqk fnryqk
     * @return 结果
     */
    @Override
    @Transactional
    public int updateFnryqk(Fnryqk fnryqk,String ppFanningdi,String ppGaofengxiandis)
    {
        int ok = 1;

        String mmFnryqkid = fnryqk.getId();

        Fanningdi mmFanningdi = new Fanningdi();
        mmFanningdi.setFnryqkid(mmFnryqkid);

        List<Fanningdi> mmFndList = ddFanningdiMapper.selectFanningdiList(mmFanningdi);
        if (mmFndList.size()==1){
            String mmFndId = mmFndList.get(0).getId();
            int ok1 = ddFanningdiMapper.deleteFanningdiById(mmFndId);
            if (ok1!=1){
                ok = 0;
            }
        }

        mmFanningdi.setId(UUID.randomUUID().toString());
        mmFanningdi.setFanningdi(ppFanningdi);
        mmFanningdi.setChuangjiantime(new Date());
        mmFanningdi.setStatus("1");
        int ok2 = ddFanningdiMapper.insertFanningdi(mmFanningdi);
        if (ok2!=1){
            ok=0;
        }

        Zhonggaofengxianqu mmZgd = new Zhonggaofengxianqu();
        mmZgd.setFnryqkid(mmFnryqkid);
        List<Zhonggaofengxianqu> mmZgdList = ddZhonggaofengxianquMapper.selectZhonggaofengxianquList(mmZgd);
        if (mmZgdList.size()==1){
            String mmZgdId = mmZgdList.get(0).getId();
            int ok3 = ddZhonggaofengxianquMapper.deleteZhonggaofengxianquById(mmZgdId);
            if (ok3!=1){
                ok = 0;
            }
        }

        String[] gaofengxiandi = ppGaofengxiandis.split(";");
        if (gaofengxiandi !=null && gaofengxiandi.length !=0){
            for (int i = 0;i<gaofengxiandi.length;i++){
                Zhonggaofengxianqu mmZhonggaofengxianqu = new Zhonggaofengxianqu();
                mmZhonggaofengxianqu.setId(UUID.randomUUID().toString());
                mmZhonggaofengxianqu.setFnryqkid(mmFnryqkid);
                mmZhonggaofengxianqu.setZhonggaofengxiandi(gaofengxiandi[i]);
                mmZhonggaofengxianqu.setChuangjiantime(new Date());
                mmZhonggaofengxianqu.setStatus("1");
                int ok4 = ddZhonggaofengxianquMapper.insertZhonggaofengxianqu(mmZhonggaofengxianqu);
                if (ok4!=1){
                    ok=0;
                }
            }
        }

        int ok5 = fnryqkMapper.updateFnryqk(fnryqk);
        if (ok5!=1){
            ok = 0;
        }

        return ok;
    }

    /**
     * 删除fnryqk对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteFnryqkByIds(String ids)
    {
        return fnryqkMapper.deleteFnryqkByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除fnryqk信息
     * 
     * @param id fnryqkID
     * @return 结果
     */
    @Override
    public int deleteFnryqkById(String id)
    {
        return fnryqkMapper.deleteFnryqkById(id);
    }

    /**
     * 查询个人fnryqk列表
     *
     * @param fnryqk fnryqk
     * @return fnryqk
     */
    @Override
    public List<Map<String,Object>> selectCustomFnryqkList(FnryqkSelect fnryqk)
    {
        List<Map<String,Object>> mmList = new ArrayList<>();

        List<Map<String,Object>> mmFnryqkList = fnryqkMapper.selectCustomFnryqkList(fnryqk);
        if (mmFnryqkList.size()!=0){
            for (int i = 0;i<mmFnryqkList.size();i++){
                Map<String,Object> mmMap = new HashMap<>();

                String mmFnryqkid = mmFnryqkList.get(i).get("id").toString();
                Fanningdi mmFanningdi = new Fanningdi();
                mmFanningdi.setFnryqkid(mmFnryqkid);

                List<Fanningdi> mmFanningdiList = ddFanningdiMapper.selectFanningdiList(mmFanningdi);
                String fnd = "";
                for (int j = 0;j<mmFanningdiList.size();j++){
                    String mmFnd = mmFanningdiList.get(j).getFanningdi();
                    fnd += mmFnd + "\n";
                }
                mmFnryqkList.get(i).put("fnd",fnd);

                Zhonggaofengxianqu mmZhonggao = new Zhonggaofengxianqu();
                mmZhonggao.setFnryqkid(mmFnryqkid);

                List<Zhonggaofengxianqu> mmZhonggaoList = ddZhonggaofengxianquMapper.selectZhonggaofengxianquList(mmZhonggao);
                String zgd = "";
                for (int k = 0;k<mmZhonggaoList.size();k++){
                    String mmZgd = mmZhonggaoList.get(k).getZhonggaofengxiandi();
                    zgd += mmZgd + "\n";
                }
                mmFnryqkList.get(i).put("zgd",zgd);
            }
        }

        return mmFnryqkList;
    }

    /**
     * 查询个人fnryqk列表
     *
     * @param fnryqk fnryqk
     * @return fnryqk
     */
    @Override
    public List<FnryqkCustom> ExportCustom(FnryqkSelect fnryqk)
    {
        List<FnryqkCustom> mmList = new ArrayList<>();

        List<Map<String,Object>> mmFnryqkList = fnryqkMapper.selectCustomFnryqkList(fnryqk);

        if (mmFnryqkList.size()!=0){
            for (int i = 0;i<mmFnryqkList.size();i++){
                String mmFnryqkid = mmFnryqkList.get(i).get("id").toString();
                Fanningdi mmFanningdi = new Fanningdi();
                mmFanningdi.setFnryqkid(mmFnryqkid);

                List<Fanningdi> mmFanningdiList = ddFanningdiMapper.selectFanningdiList(mmFanningdi);
                String fnd = "";
                for (int j = 0;j<mmFanningdiList.size();j++){
                    String mmFnd = mmFanningdiList.get(j).getFanningdi();
                    fnd += mmFnd + "\n";
                }

                Zhonggaofengxianqu mmZhonggao = new Zhonggaofengxianqu();
                mmZhonggao.setFnryqkid(mmFnryqkid);

                List<Zhonggaofengxianqu> mmZhonggaoList = ddZhonggaofengxianquMapper.selectZhonggaofengxianquList(mmZhonggao);
                String zgd = "";
                for (int k = 0;k<mmZhonggaoList.size();k++){
                    String mmZgd = mmZhonggaoList.get(k).getZhonggaofengxiandi();
                    zgd += mmZgd + "\n";
                }

                FnryqkCustom mmFnryqkCustom = new FnryqkCustom();
                mmFnryqkCustom.setName(mmFnryqkList.get(i).get("truename")==null?"":mmFnryqkList.get(i).get("truename").toString());
                mmFnryqkCustom.setKeshiname(mmFnryqkList.get(i).get("keshiname")==null?"":mmFnryqkList.get(i).get("keshiname").toString());
                mmFnryqkCustom.setFanningtime(mmFnryqkList.get(i).get("fanningshijian")==null?"":mmFnryqkList.get(i).get("fanningshijian").toString());
                mmFnryqkCustom.setFanningdi(fnd);
                mmFnryqkCustom.setJiaotongfangshi(mmFnryqkList.get(i).get("jiaotongfangshi")==null?"":mmFnryqkList.get(i).get("jiaotongfangshi").toString());
                mmFnryqkCustom.setZhonggaodi(zgd);
                mmFnryqkCustom.setRenyuanleibie(mmFnryqkList.get(i).get("leibiename")==null?"":mmFnryqkList.get(i).get("leibiename").toString());
                mmFnryqkCustom.setYanse(mmFnryqkList.get(i).get("yanse")==null?"":mmFnryqkList.get(i).get("yanse").toString());
                mmFnryqkCustom.setDianhua(mmFnryqkList.get(i).get("dianhua")==null?"":mmFnryqkList.get(i).get("dianhua").toString());
                mmFnryqkCustom.setBeizhu(mmFnryqkList.get(i).get("beizhu")==null?"":mmFnryqkList.get(i).get("beizhu").toString());

                mmList.add(mmFnryqkCustom);
            }
        }

        return mmList;
    }

}
